-- BraveCertificate
-- Created by chengb Aug/2/2016
-- 勇者之证

-- 初始化
function initBraveCertificate(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local elementDbase = DungeonM.queryElement(self.gridData.element, "dbase");

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local id = BraveVSDragonM.queryLockIdByAlias("brave_certificate");
            if not BraveVSDragonM.isBuidingUnlock(id) then
                -- 建筑尚未解锁
                alert(getLocStr("cannot_arrive"));
                return;
            end

            local function okFunc()
                DungeonActionM.go("brave_building_bonus", self.gridData:getPos());
            end

            AudioM.playFx("ui_open1");
            self:openBuildingView(okFunc);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    local isVisible = false;

    -- 如果还没领取奖励
    if not BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
        -- 如果配置了需要穿装备才显示
        if type(elementDbase["require_equip"]) == 'number' then
            local classId = elementDbase["require_equip"];
            if EquipM.isEquiped(classId) then
                isVisible = true;
            end
        else
            isVisible = true;
        end
    end

    self.icon:setVisible(isVisible);
end

